package com.wip.reportingclient.ui.controllers;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wip.reportingclient.services.domains.ListData;
import com.wip.reportingclient.services.domains.ReportFormat;
import com.wip.reportingclient.services.domains.ReportTypeEnum;
import com.wip.reportingclient.services.interfaces.IWebService;
import com.wip.reportingclient.ui.components.FormData;
import com.wip.reportingclient.ui.validators.FormDataValidator;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/")
@Scope("session")
public class HomeController {

	/*
	 * private static final Logger logger = LoggerFactory
	 * .getLogger(HomeController.class);
	 */

	@Autowired
	private FormData formData;

	@Autowired
	private IWebService service;

	@Autowired
	private FormDataValidator validator;

	private HashMap<String, Object> map;// = new HashMap<String, Object>();

	/**
	 * Simply selects the home view to render by returning its name.
	 */

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home() {
		// logger.info(".../client/");

		// service.getData(request, response);

		// return "home";

		ListData clientlist = service.fetchAllClients();

		Map<String, String> clients = new LinkedHashMap<String, String>();

		for (String client : clientlist.getData()) {
			clients.put(client, client);
		}

		ListData loglList = service.fetchAllLogs();

		Map<String, String> logs = new LinkedHashMap<String, String>();

		for (String log : loglList.getData()) {
			logs.put(log, log);
		}

		ListData pagelList = service.fetchAllPages();

		Map<String, String> pages = new LinkedHashMap<String, String>();

		for (String page : pagelList.getData()) {
			pages.put(page, page);
		}

		// ListData pagelList = service.fetchAllPages();

		Map<String, String> reportTypes = new LinkedHashMap<String, String>();

		for (int i = 0; i < ReportTypeEnum.values().length; i++) {

			reportTypes.put(ReportTypeEnum.values()[i].getReportType(),
					ReportTypeEnum.values()[i].getReportType());
		}

		map = new HashMap<String, Object>();
		map.put("formData", formData);
		map.put("clientsList", clients);
		map.put("pagesList", pages);
		map.put("logsList", logs);
		map.put("reportTypesList", reportTypes);

		return new ModelAndView("home", map);

		// return new ModelAndView("home", "formData", new FormData());

	}

	@RequestMapping(value = "/service", params = "pdfReport", method = RequestMethod.POST)
	public void pdfExport(
			@ModelAttribute("formData") FormData formData,
			HttpServletRequest request, HttpServletResponse response,
			BindingResult result) {
		// logger.info(".../client/service");

		validator.validate(formData, result);

		if (!result.hasErrors())
			service.generateReport(request, response, ReportFormat.PDF,
					formData);

		// return "home";
		// return new ModelAndView("home");
	}

	@RequestMapping(value = "/service", params = "xlsReport", method = RequestMethod.POST)
	public void  xlsExport(
			  @ModelAttribute("formData") FormData formData,
			HttpServletRequest request, HttpServletResponse response,
			BindingResult result) {

		// validate formData.
		validator.validate(formData, result);

		if (!result.hasErrors())
			service.generateReport(request, response, ReportFormat.XLS,
					formData);

		// return "home";
		//return new ModelAndView("home");
		// return home();
	}

	@RequestMapping(value = "/service", params = "xlsxReport", method = RequestMethod.POST)
	public void xlsxExport(
			@ModelAttribute("formData") FormData formData,
			HttpServletRequest request, HttpServletResponse response,
			BindingResult result) {

		// validate formData.
		validator.validate(formData, result);

		if (!result.hasErrors())
			service.generateReport(request, response, ReportFormat.XLSX,
					formData);
		// else
		// return "home";
		// return new ModelAndView("home");
	}

}
